﻿/* 
 * Since 2008 - 2012
 */
package com.hh.bss.project.controller;

import static com.hh.bss.util.extjs.JsonHelper.outJson;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

import com.hh.bss.base.*;
import com.hh.bss.common.page.*;
import com.hh.bss.common.page.impl.*;
import com.hh.bss.project.dao.*;
import com.hh.bss.project.entity.*;
import com.hh.bss.project.service.*;
import com.hh.bss.project.vo.query.*;
import com.hh.bss.util.*;
import com.hh.bss.util.extjs.ExtJsPageHelper;
import com.hh.bss.util.extjs.ListRange;

import java.util.*;




@Controller
public class ContractJobOfferPartnerController extends BaseSpringController {
	// 默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null;

	private ContractJobOfferPartnerService contractJobOfferPartnerService;

	private ContractJobOfferPartner contractJobOfferPartner;
	java.lang.Integer id = null;
	private String[] items;

	/** 通过spring自动注入 */
	public void setContractJobOfferPartnerServiceImpl(ContractJobOfferPartnerService service) {
		this.contractJobOfferPartnerService = service;
	}

	public void setId(java.lang.Integer val) {
		this.id = val;
	}

	public void setItems(String[] items) {
		this.items = items;
	}

	/**
	 * ExtGrid使用 列表
	 * 
	 * @throws IOException
	 */
	public void list(HttpServletRequest request, HttpServletResponse response, ContractJobOfferPartnerQuery contractJobOfferPartnerQuery) throws IOException {
		PageRequest<Map> pr = ExtJsPageHelper.createPageRequestForExtJs(request, DEFAULT_SORT_COLUMNS);
		Page page = contractJobOfferPartnerService.findPage(contractJobOfferPartnerQuery);

		List<ContractJobOfferPartner> ContractJobOfferPartnerlist = (List) page.getResult();
		ListRange<ContractJobOfferPartner> resultList = new ListRange<ContractJobOfferPartner>();
		resultList.setRows(ContractJobOfferPartnerlist);
		resultList.setTotal(page.getTotalCount());
		resultList.setMessage("ok");
		resultList.setSuccess(true);
		outJson(response, resultList);
	}
	public void get(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Map<String, Object> result = new HashMap<String, Object>();
		String id = request.getParameter("id");
		if (StringUtils.isNotEmpty(id)) {
			result.put("failure", true);
			result.put("msg", "参数非法!");
		}
		try {
			ContractJobOfferPartner data = (ContractJobOfferPartner) contractJobOfferPartnerService.getById(Integer.parseInt(id));
			if (data != null) {
				result.put("data", data);
				result.put("success", true);
				result.put("msg", "成功!");
			}

		} catch (Exception e) {
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response, result);
	}
	 public String managerPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
			
			return "forward:/jsp/project/contractJobOfferPartner/contractJobOfferPartnerPage.jsp";
			
		}

	/**
	 * extGrid保存
	 * 
	 * @throws IOException
	 */
	public void save(HttpServletRequest request, HttpServletResponse response, ContractJobOfferPartner contractJobOfferPartner) throws IOException {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			// 设置创建用户和创建时间
			com.hh.bss.auth.client.OnlineManager onlineManager = super.getOnlineManager(request, response);
			if (onlineManager != null) {
				com.hh.bss.auth.client.OnlineUser onlineUser = onlineManager.getOnlineUser();
				if (onlineUser != null && com.hh.bss.util.BeanUtils.isExistsProperty(ContractJobOfferPartner.class, "creator")) {
					try {
						com.hh.bss.util.BeanUtils.setProperty(contractJobOfferPartner, "creator", onlineUser.getUsername());
					} catch (Exception ex) {
					}
				}
			}

			if (com.hh.bss.util.BeanUtils.isExistsProperty(ContractJobOfferPartner.class, "createDt")) {
				try {
					com.hh.bss.util.BeanUtils.setProperty(contractJobOfferPartner, "createDt", new Date());
				} catch (Exception ex) {
				}
			}

			contractJobOfferPartnerService.save(contractJobOfferPartner);
			result.put("success", true);
			result.put("msg", "添加成功!");
		} catch (Exception e) {
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response, result);
	}

	/**
	 * extGrid修改
	 * 
	 * @throws IOException
	 */
	public void update(HttpServletRequest request, HttpServletResponse response, ContractJobOfferPartner contractJobOfferPartner) throws IOException {
		Map<String, Object> result = new HashMap<String, Object>();
		try {

			logger.info("方法调用之前.....");
			if (StringUtils.isNotEmpty(contractJobOfferPartner.getRole())) {
				// 如果是 职位负责人 和 合作顾问 验证业绩
				if ("MC".equals(contractJobOfferPartner.getRole()) || "PC".equals(contractJobOfferPartner.getRole())) {
					if (!contractJobOfferPartnerService.validateOfferPartner(contractJobOfferPartner)) {
						logger.info("方法调用之中......");
						result.put("failure", true);
						result.put("msg", "职位负责人和合作顾问的业绩总和不得超过 100");
						outJson(response, result);
						return;
					}
					logger.info("方法调用完成.....");
				}
			}

			ContractJobOfferPartner oldcontractJobOfferPartner = (ContractJobOfferPartner) contractJobOfferPartnerService.getById(contractJobOfferPartner.getId());

			bind(request, oldcontractJobOfferPartner);
			// 设置修改用户和修改时间
			com.hh.bss.auth.client.OnlineManager onlineManager = super.getOnlineManager(request, response);
			if (onlineManager != null) {
				com.hh.bss.auth.client.OnlineUser onlineUser = onlineManager.getOnlineUser();
				if (onlineUser != null && com.hh.bss.util.BeanUtils.isExistsProperty(ContractJobOfferPartner.class, "modifier")) {
					try {
						com.hh.bss.util.BeanUtils.setProperty(oldcontractJobOfferPartner, "modifier", onlineUser.getUsername());
					} catch (Exception ex) {
					}
				}
			}

			if (com.hh.bss.util.BeanUtils.isExistsProperty(ContractJobOfferPartner.class, "modifiedDt")) {
				try {
					com.hh.bss.util.BeanUtils.setProperty(oldcontractJobOfferPartner, "modifiedDt", new Date());
				} catch (Exception ex) {
				}
			}

			/*
			 * if(contractJobOfferPartnerService.validateOfferPartner(
			 * contractJobOfferPartner)){
			 * contractJobOfferPartnerService.update(oldcontractJobOfferPartner
			 * ); result.put("success", true); result.put("msg", "修改成功!");
			 * }else{ result.put("failure", true); result.put("msg",
			 * "职位负责人和合作顾问的业绩总和不得超过 100"); }
			 */
			
			// 验证权限(职位参与人才能修改)
			if (contractJobOfferPartnerService.isJobOfferPartner(oldcontractJobOfferPartner)) {
				contractJobOfferPartnerService.update(oldcontractJobOfferPartner);
				result.put("success", true);
				result.put("msg", "修改成功!");

			} else {
				result.put("failure", true);
				result.put("msg", "对不起,你不是参与人,不能修改此记录");
			}

		} catch (Exception e) {
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response, result);
	}

	/**
	 * extGrid删除
	 * 
	 * @throws IOException
	 */
	public void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String ids = request.getParameter("ids");
		String[] idarray = ids.split(",");
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			for (int i = 0; i < idarray.length; i++) {
				java.lang.Integer id = new java.lang.Integer((String) idarray[i]);
				contractJobOfferPartnerService.removeById(id);
			}
			result.put("success", true);
			result.put("msg", "删除成功");
		} catch (Exception e) {
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response, result);
	}

}
